<?php
/**
 * 
 * Attachment(附件管理)
 * $Id: AttachmentAction.class.php 1615 2013-01-29 02:04:32Z IICC $
 */
class AttachmentAction extends  Action {
	protected $dao,$isadmin=0,$userid=0,$groupid=0;
    function _initialize(){	
		if($_POST['PHPSESSID'] && $_POST['swf_auth_key'] && $_POST['userid']){
			if($_POST['swf_auth_key']==sysmd5($_POST['PHPSESSID'].$_POST['userid'])){
				$this->userid = $_POST['userid'];
			}
		}		
		if(!$this->userid){
			import('@.Action.Base');
			$Base=new BaseAction();
			$Base->_islogin();
			$this->userid=$Base->userid;
			$roles=$Base->roles;
		}
		$this->dao=M('Attachment');
    }
	public function index(){
		$auth = str_replace(' ','+',$_REQUEST['auth']) ;
		$postd=array('isadmin','more','isthumb','file_limit','file_types','file_size','moduleid');
		foreach((array)$_REQUEST as $key=>$res){
			if(in_array($key,$postd))$postdata[$key]=$res;
		}
		$upsetup = implode('-',$postdata);
		$iic_auth_key = sysmd5(C('ADMIN_ACCESS').$_SERVER['HTTP_USER_AGENT']);
		$enupsetup = authcode($auth, 'DECODE', $iic_auth_key);
		if(!$enupsetup || $upsetup!=$enupsetup)  $this->error ('参数错误');
		$sessid = time();
		$count = $this->dao->where('status=0 and userid ='.$this->userid)->count();
		$this->assign('no_use_files',$count);
		$this->assign('small_upfile_limit',$_REQUEST['file_limit'] - $count);
		$types = '*.'.str_replace(",",";*.",$_REQUEST['file_types']); ;
		$this->assign('moduleid',$_REQUEST['moduleid']);
		$this->assign('file_size',$_REQUEST['file_size']);
		$this->assign('file_limit',$_REQUEST['file_limit']);
		$this->assign('file_types',$types);
		$this->assign('isthumb',$_REQUEST['isthumb']);
		$this->assign('sessid',$sessid);
		$this->assign('userid',$this->userid);
		$swf_auth_key = sysmd5($sessid.$this->userid);
		$this->assign('swf_auth_key',$swf_auth_key);
		$this->assign('more',$_GET['more']);
		$this->display();
	}

	public function upload(){
		//if($_POST['swf_auth_key']!= sysmd5($_POST['PHPSESSID'].$this->userid)) $this->ajaxReturn(0,'1-'.$_POST['PHPSESSID'],0);
		//iic
		$md5file = md5_file ( $_FILES ['filedata'] ['tmp_name'] );
		//F('sess',$md5file);
		/*$dao = M ( "Attachment" );
		$condition = array ("hash" => $md5file );
		$info = array ();
		$info = $dao->where ( $condition )->find ();
		if ($info) {
			$returndata ['aid'] = $info ['aid'];
			$returndata ['fileext'] = $info ['fileext'];
			if($info['isthumb']&&$_REQUEST['isthumb']){
				$info['filepath'] = $info['filepath'].$info['thumbSuffix'].'.'.$info['fileext'];
			}
			$returndata ['filepath'] = $info['filepath'];
			$returndata ['isimage'] = $info ['isimage'];
			$returndata ['filename'] = $info ['filename'];
			$returndata ['filesize'] = $info ['filesize'];
			$this->ajaxReturn ( $returndata,'上传成功', '1' );
		}*/
		import("ORG.Net.UploadFile"); 
        $upload = new UploadFile(); 
		//$upload->supportMulti = false;
        //设置上传文件大小 
        $upload->maxSize = $this->Config['attach_maxsize']; 
		$upload->autoSub = true; 
		$upload->subType = 'date';
		$upload->dateFormat = 'Ym';
        //设置上传文件类型 
        $upload->allowExts = explode(',', $this->Config['attach_allowext']); 
        //设置附件上传目录 
        $upload->savePath = UPLOAD_PATH; 
		 //设置上传文件规则 
        $upload->saveRule = uniqid; 
        //删除原图 
        $upload->thumbRemoveOrigin =false; 
        if (!$upload->upload()) { 
			$this->ajaxReturn(0,$upload->getErrorMsg(),0);
        } else { 
            //取得成功上传的文件信息 
            $uploadList = $upload->getUploadFileInfo(); 
			
			if($_REQUEST['addwater']){ //$this->Config['watermark_enable']  $_REQUEST['addwater']
				import("ORG.Util.Image");
				Image::watermark($uploadList[0]['savepath'].$uploadList[0]['savename'],'',$this->Config);
			}
			if($_REQUEST['isthumb']){
			    /*$img=__ROOT__.substr($uploadList[0]['savepath'].strtolower($uploadList[0]['savename']),1);
    			$img=str_replace('./', '/', $img);
                $pathinfo = pathinfo($img);
                $url=WEB_PATH.str_replace('/','\\',$img);
                $lit=WEB_PATH.str_replace('/','\\',$pathinfo['dirname']).'\\';
                $thumbSuffix=$z='!fh160X160.'.$pathinfo['extension'];
                $m=new COM("jwcls.jwimg");
                $sx=$m->goSB($lit,$url,$w,$h,$z);
                $sx=$pathinfo['dirname'].'/'.$sx;
                 */
                $thumbSuffix=''; 
            }else{
                $thumbSuffix='';
            }
			$imagearr = explode(',', 'jpg,gif,png,jpeg,bmp,ttf,tif'); 
			$data=array();
			//$model = M('Attachment');
			//保存当前数据对象
			$data['moduleid'] = $_REQUEST['moduleid'];
			$data['catid'] = 0;
			$data['userid'] = $_REQUEST['userid'];
			$data['filename'] = $uploadList[0]['name'];
			$data['isthumb'] = intval($_REQUEST['isthumb']);
			$data['thumbSuffix'] =$thumbSuffix;
			$data['fileext'] = $uploadList[0]['extension'];
			$data['filepath'] = __ROOT__.substr($uploadList[0]['savepath'].$uploadList[0]['savename'],1);
			$data['filesize'] = $uploadList[0]['size']; 
			$data['isimage'] = in_array($data['fileext'],$imagearr) ? 1 : 0;
			$data['createtime'] = time();
			$data['hash'] = $md5file;
			$data['short']= short($md5file);
			$data['uploadip'] = get_client_ip();
// 			$aid = $model->add($data); 
// 			$returndata['aid']		= $aid;
			//$returndata['aid']		= $model->getLastSql();
			if($_REQUEST['isthumb']){
				$returndata['filepath'] = $data['filepath'].$thumbSuffix;
			}else{
				$returndata['filepath'] = $data['filepath'];
			}
			$returndata['fileext']  = $data['fileext'];
			$returndata['isimage']  = $data['isimage'];
			$returndata['filename'] = $data['filename'];
			$returndata['filesize'] = $data['filesize']; 
			$this->ajaxReturn($returndata,'上传成功', '1');
        }	
	}

	public function filelist(){

		$where= $_REQUEST['typeid'] ?  " status=1 " : " status=0 ";
		$where .=" and userid = ".$this->userid ;
		import("ORG.Util.Page");
		$count = $this->dao->where($where)->count();
		$page=new Page($count,12); 
		$imagearr = explode(',', 'jpg,gif,png,jpeg,bmp,ttf,tif'); 

		$page->urlrule = 'javascript:ajaxload('.$_REQUEST['typeid'].',{$page},\''.$_REQUEST['inputid'].'\','.$this->isadmin.');';
		$show = $page->show(); 
		$this->assign("page",$show);
		$list=$this->dao->order('aid desc')->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		foreach((array)$list as $key=>$r){
		$list[$key]['thumb']=in_array($r['fileext'],$imagearr) ? $r['filepath'] : __ROOT__.'/Public/Images/ext/'.$r['fileext'].'.png'; 
		}
		$this->assign('list',$list);
		$this->display();
	}

	function delfile($aid){
		if(empty($aid)){
		$aid=$_REQUEST['aid'];
		}
		$r = delattach(array('aid'=>$aid,'userid'=>$this->userid));
		if($r){		 
			$this->success ( L ( 'delete_ok' ) );
		}else{
			$this->error ( L ( 'delete_error' ) );
		}
	
	}
	function cleanfile(){
		$r = delattach(array('status'=>0,'userid'=>$this->userid));
		if($r){		 
			$this->success ( L ( 'delete_ok' ) );
		}else{
			$this->error ( L ( 'delete_error' ) );
		}
	}
	
	function faust() {
		$this->display();
	}
	
}
?>